package com.mobilekites.activities;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.TextUtils;

import com.android.volley.VolleyError;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.mobilekites.R;
import com.mobilekites.api.model.DfeAppConfig;
import com.mobilekites.entity.AppConfig;
import com.mobilekites.gcm.UpdateController;
import com.mobilekites.gcm.UpdateController.OnUpdateSelected;
import com.mobilekites.service.KitesGCMService;

public class SplashActivity extends KitesActivity {
	private DfeAppConfig mDfeAppConfig;
	private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (checkPlayServices()) {
			String regId = getKitesPrefers().getRegistrationIdGCM();
			if (TextUtils.isEmpty(regId)) {
				KitesGCMService.startRegisterGCM(getApplicationContext());
			}
		}
		checkUpdate();
	}

	@Override
	protected void setContentView() {
		setContentView(getLayoutRes());
	}

	@Override
	protected int getLayoutRes() {
		return R.layout.activity_splash;
	}

	@Override
	protected void requestData() {
		if (mDfeAppConfig == null) {
			mDfeAppConfig = new DfeAppConfig(getDfeApi());
			mDfeAppConfig.addDataChangedListener(this);
			mDfeAppConfig.addErrorListener(this);
		}
		mDfeAppConfig.startLoadData();
	}

	@Override
	public void onDataChanged() {
		super.onDataChanged();
		mDfeAppConfig.removeDataChangedListener(this);
		mDfeAppConfig.removeErrorListener(this);
		AppConfig appConfig = mDfeAppConfig.getAppConfig();
		getKitesPrefers().saveAbout(appConfig.about);
		getKitesPrefers().saveShowMoreApp(appConfig.isShowMoreApp);
		getKitesPrefers().saveAdmob(appConfig.admob);
		getKitesPrefers().saveOtherAppUrl(appConfig.urlMore);
	}

	private boolean isReady() {
		return mDfeAppConfig != null && mDfeAppConfig.isReady() ? true : false;
	}

	@Override
	protected void onResume() {
		super.onResume();
		checkPlayServices();
	}

	@Override
	public void onErrorResponse(VolleyError volleyerror) {
		super.onErrorResponse(volleyerror);
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle(R.string.kites_notify);
		builder.setMessage(volleyerror.getMessage());
		builder.setPositiveButton(R.string.kites_ok, new OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				finish();
			}
		});
		builder.setCancelable(false);
		builder.show();
	}

	private boolean checkPlayServices() {
		int resultCode = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(this);
		if (resultCode != ConnectionResult.SUCCESS) {
			if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
				GooglePlayServicesUtil.getErrorDialog(resultCode, this,
						PLAY_SERVICES_RESOLUTION_REQUEST).show();
			}
			return false;
		}
		return true;
	}

	private void checkUpdate() {
		UpdateController updateController = new UpdateController(this);
		updateController.execute(this, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					onUpdateSelected.onAcceptUpdate();
					break;
				case DialogInterface.BUTTON_NEGATIVE:
					onUpdateSelected.onIgnoreUpdate();
					break;
				case DialogInterface.BUTTON_NEUTRAL:
					onUpdateSelected.onIgnoreUpdate();
					break;
				default:
					break;
				}
			}
		}, onUpdateSelected);
	}

	private OnUpdateSelected onUpdateSelected = new OnUpdateSelected() {

		@Override
		public void onIgnoreUpdate() {
			if (!isReady()) {
				switchToLoading();
				requestData();
			}
		}

		@Override
		public void onAcceptUpdate() {
		}
	};
}
